<template>
    <!-- 修改资料 -->
    <div>
        <!-- 内容主题区域 -->
        <el-form label-width="75px" :model="form" label-position="right" ref="formRef" :rules="formRules">
            <el-row>
                <el-col :xs="24" :sm="24" :md="24" :lg="12" :xl="12">
                    <el-form-item label="模板标识" prop="number">
                        <el-input style="width:70%" v-model="form.number" placeholder="请输入模板标识"></el-input>&nbsp;
                    </el-form-item>
                </el-col>
                <el-col :xs="24" :sm="24" :md="24" :lg="12" :xl="12">
                    <el-form-item label="模板名称" prop="name">
                        <el-input v-model="form.name" placeholder="请输入模板名称"></el-input>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row>
                <el-col :xs="24" :sm="24" :md="24" :lg="12" :xl="12">
                    <el-form-item label="功能模块" prop="metaModuleId">
                        <ts-select v-model="form.metaModuleId" fileName="MetaModuleReport" :template="tableProps"
                            @updateRow="metaModuleChange"></ts-select>
                    </el-form-item>
                </el-col>
                <el-col :xs="24" :sm="24" :md="24" :lg="12" :xl="12">
                    <el-form-item label="报表文件名">
                        <el-select v-model="form.reportFileName" clearable placeholder="请选择：">
                            <el-option v-for="item in reportFileNameList" :key="item.value" :label="item.label"
                                :value="item.value">
                            </el-option>
                        </el-select>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row>
                <el-col :xs="24" :sm="24" :md="24" :lg="12" :xl="12">
                    <el-form-item label="上传附件" prop="file">
                        <tsUpload type="file" :image-url="imageUrl" @updateFile="updateFile" @removeFile="removeFile">
                        </tsUpload>
                    </el-form-item>
                </el-col>
                <el-col :xs="24" :sm="24" :md="24" :lg="12" :xl="12">
                    <el-form-item label="状态" prop="status">
                        <el-switch v-model="form.status" active-value="normal" inactive-value="disable"></el-switch>
                    </el-form-item>
                </el-col>
            </el-row>

            <el-form-item label="备注" prop="remark">
                <el-input v-model="form.remark" type="textarea"></el-input>
            </el-form-item>

            <sc-title title="表字段列表"></sc-title>
            <el-scrollbar height="400px">
                <sc-form-table v-model="dataList" :addTemplate="addTemplate" edit @delete="rowDelete"
                    placeholder="暂无数据">
                    <template #tableContent>
                        <el-table-column prop="number" label="数据集标识">
                            <template #default="scope">
                                <span v-if="scope.row.operationType == ''">{{ scope.row.number }}</span>
                                <el-input v-else v-model="scope.row.number" placeholder="请输入数据集标识"></el-input>
                            </template>
                        </el-table-column>
                        <el-table-column prop="name" label="名称">
                            <template #default="scope">
                                <span v-if="scope.row.operationType == ''">{{ scope.row.name }}</span>
                                <el-input v-else v-model="scope.row.name" placeholder="请输入名称"></el-input>
                            </template>
                        </el-table-column>
                        <el-table-column label="字段类型">
                            <template #default="scope">
                                <span v-if="scope.row.operationType == ''">{{ scope.row.datasetTypeName }}</span>
                                <el-select v-else v-model="scope.row.datasetType" clearable placeholder="请选择：">
                                    <el-option v-for="item in configData.datasetTypeList" :key="item.value"
                                        :label="item.label" :value="item.value">
                                    </el-option>
                                </el-select>
                            </template>
                        </el-table-column>
                    </template>
                </sc-form-table>
            </el-scrollbar>
        </el-form>
        <div class="button-position">
            <el-button @click="updateVisible">取 消</el-button>
            <el-button type="primary" @click="updatePage(formRef)">确 定</el-button>
        </div>
    </div>
</template>

<script setup>
import { reactive, ref, onMounted } from 'vue';
import { ElNotification } from 'element-plus';
import { Check, Close } from '@element-plus/icons-vue';
import Api from '@/api/model/meta'
import tsSelect from '@/components/scSelect/index.vue'
import tsUpload from '@/components/tsUpload/file.vue'
const emit = defineEmits(['updatetable'])
// 添加用户数据的对象
const form = ref({});
const configData = ref({})
const addTemplate = ref({})
const dataList = ref([])
const deleteList = ref([])
const formRef = ref()
const tableProps = {
    value: 'id',
    label: 'name'
}
const imageUrl = reactive({
    name: '',
    url: ''
})
const reportFileNameList = ref([])
const props = defineProps({
    paramsData: {
        type: Object,
        defalut: {}
    }
})
onMounted(() => {
    getInit()
})

const getInit = async () => {
    let formRes = await Api.pageTemplate.detail.post(props.paramsData)
    if (formRes.code == 200) {
        imageUrl.url = formRes.data.fileFullPath
        imageUrl.name = formRes.data.fileName
        form.value = formRes.data
        configData.value = formRes.configData
        addTemplate.value = formRes.dtl
        dataList.value = formRes.dataList
    }
}
const rowDelete = (val) => {
    deleteList.value.push(val)
}

const restAddFormData = () => {
    formRef.value.resetFields();
}

const updateVisible = (val) => {
    emit('updatetable')
}
const updateFile = (val) => {
    form.value.file = val
}
const removeFile = () => {
    form.value.file = undefined
}
const metaModuleChange =async(val) => {
    console.log(val)
    let res =await Api.pageTemplate.searchConfigFiles.post({name:val.id})
    if(res.code == 200){
        console.log(res)
        reportFileNameList.value = res.dataList
    }
}
// 上传
const updatePage = async (formRef) => {
    await formRef.validate((valid, fields) => {
        if (valid) {
            updatePageData();
        }
    })
}

const updatePageData = async () => {
    let params = {
        bean: form.value,
        dataList: dataList.value.concat(deleteList.value)
    }
    let res = await Api.pageTemplate.update.post(params);
    if (res.code == 200) {
        restAddFormData()
        ElNotification.success({ title: '', message: '添加成功！', duration: 2000 });
        emit('updatetable', true)
    }
}

</script>

<style lang="less" scoped></style>
